package com.twu.biblioteca;

import java.util.ArrayList;

public class MenuState implements State{

    public State executeStateBehaviour(UserInterface userInterface, Database database) {
        writeMenuList(userInterface);
        return getNextState(userInterface);
    }

    private void writeMenuList(UserInterface userInterface) {
        userInterface.write(new Output("Please Select a Menu Option by Number:"));
        userInterface.write(new Output("--------------------------------------"));
        userInterface.write(new Output(new MenuList()));
        userInterface.write(new Output("--------------------------------------"));
    }

    private State getNextState(UserInterface userInterface) {

        try {

            int selection = Integer.parseInt(userInterface.read().string());
            new MenuList().get(selection-1);

            switch (selection) {
                case 1:
                    return new LoginState();
                case 2:
                    return new BookListState();
                case 3:
                    return new MovieListState();
                case 4:
                    return new ReservationState();
                case 5:
                    return new MembershipState();
                case 6:
                    return null;
                default:
                    throw new RuntimeException("Illegal value returned from getUserMainMeuOptionSelection()");
            }

        } catch (NumberFormatException e) {
            userInterface.write(new Output("Invalid Format of Input."));
            return new MenuState();
        } catch (IndexOutOfBoundsException e) {
            userInterface.write(new Output("Invalid Menu Option Number."));
            return new MenuState();
        }

    }

}